<?php

namespace app\index\model;

use think\Db;
use think\Model;

class Article extends Model
{
    // 文章列表
    public function ArticleList()
    {
        return $this->table('article')
            ->alias('a')
            ->join('categorys c','a.category_id = c.id')
            ->join('user u','a.user_id = u.id')
            ->field('a.*,c.classname,u.name')
            ->order('a.addate','desc')
            ->paginate(4);
    }

    // 查询指定的一条数据
    public function ArticleFind(int $id)
    {
        return $this->table('article')
            ->alias('a')
            ->join('categorys c','a.category_id = c.id')
            ->join('user u','a.user_id = u.id')
            ->field('a.*,c.classname,u.name')
            ->where('a.id',$id)
            ->find();
    }

    // 查询指定分类数据
    public static function ArtKeyword($key)
    {
        return self::table('article')
            ->alias('a')
            ->join('categorys c','a.category_id = c.id')
            ->join('user u','a.user_id = u.id')
            ->field('a.*,c.classname,u.name')
            ->order('a.addate','desc')
            ->where('a.title','like','%'.$key.'%')
            ->paginate(3);
    }

    // 按照月份查询
    public static function Months()
    {
        return Db::query("SELECT DATE_FORMAT(FROM_UNIXTIME(addate),'%Y年%m月') AS months,count(id) AS records FROM article group by months ORDER BY months DESC");
    }

    // 上一篇下一篇  默认小于
    public static function upDpwn(int $id,$paixu="<",$order="DESC"){
        return self::table('article')
            ->alias('a')
            ->join('categorys c','a.category_id = c.id')
            ->join('user u','a.user_id = u.id')
            ->field('a.*,c.classname,u.name')
            ->where('a.id',$paixu,$id)
            ->order('a.id', $order)
            ->find();
    }
}
